__author__ = 'st316'
'''
Follow up for "Remove Duplicates":
What if duplicates are allowed at most twice?

For example,
Given sorted array A = [1,1,1,2,2,3],

Your function should return length = 5, and A is now [1,1,2,2,3].
'''


class Solution:
    # @param A a list of integers
    # @return an integer
    def removeDuplicates(self, A):
        if not A or len(A) == 0:
            return 0
        i = 1
        tmp = A[0]
        count = 1
        index = 0
        while i < len(A):
            if A[i] != tmp:
                if index + 1 != i:
                    A[index + 1] = A[i]
                index += 1
                tmp = A[i]
                count = 1
            elif count == 1:
                if index + 1 != i:
                    A[index + 1] = A[i]
                index += 1
                count = 2
            i += 1
        return index + 1


if __name__ == '__main__':
    print "12234%s%s%s" % ('x', 'y', 'z')
    # s = Solution()
    # A = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 3, 3]
    # print s.removeDuplicates(A)
    # print A